﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Legou365.Web.Models;
using HiLand.General.BLL;
using HiLand.General.Entity;
using HiLand.Utility4.MVC.Data;
using HiLand.Utility4.Web;
using HiLand.Utility.IO;
using System.IO;
using HiLand.Utility.Data;
using HiLand.Utility.Enums;
using HiLand.Utility.Entity;

namespace Legou365.Web.Areas.Manage.Controllers
{
    public class ImageController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerGuid"></param>
        /// <param name="productGuid"></param>
        /// <returns></returns>
        public ActionResult ImageList(Guid? ownerGuid, Guid productGuid)
        {
            this.PassParam<SimpleProductEntity>("currentProduct", SimpleProductBLL.Instance.Get(productGuid));
            this.BrokeParam("returnUrl");

            List<ImageEntity> ImageList = ImageBLL.Instance.GetList(ownerGuid.HasValue?ownerGuid.Value:Guid.Empty, productGuid);
            return View(ImageList);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="imageGuid"></param>
        /// <param name="productGuid"></param>
        /// <returns></returns>
        public ActionResult ImageDetails(string imageGuid = "", string productGuid = "", string returnUrl="")
        {
            ImageEntity entity = null;
            if (string.IsNullOrWhiteSpace(imageGuid) || imageGuid == Guid.Empty.ToString())
            {
                entity = new ImageEntity();
            }
            else
            {
                entity = ImageBLL.Instance.Get(imageGuid);
            }

            this.PassParam("returnUrl", returnUrl);
            this.PassParam("productGuid", productGuid);

            return View(entity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ImageDetails(ImageEntity entity,string returnUrl= "",string subDirName= "Product")
        {
            if (entity.ImageGuid != Guid.Empty)
            {
                ImageEntity entityLoaded = ImageBLL.Instance.Get(entity.ImageGuid);
                if (entityLoaded != null)
                {
                    entity.CreateTime = entityLoaded.CreateTime;
                    entity.ImageRelativePath = entityLoaded.ImageRelativePath;
                    entity.ImageType = entityLoaded.ImageType;
                }
            }

            HttpPostedFileBase fileInfo = Request.Files["fileInput"];
            if (fileInfo.HasFile())
            {
                string fileExtensionName = Path.GetExtension(fileInfo.FileName);
                string fileName = string.Format("{0}{1}", GuidHelper.NewGuidString(), fileExtensionName);
                string baseVirtualPath = PathHelper.CombineForVirtual(ImageEntity.ImageVirtualBasePath, subDirName);

                string nativeFullPath = IOHelper.EnsureDatePath(Request.MapPath(baseVirtualPath), DatePathFormaters.Y_MD);
                nativeFullPath = Path.Combine(nativeFullPath, fileName);
                string relativeVirtualPath = IOHelper.GetRelativeVirtualPath(nativeFullPath, ImageEntity.ImageVirtualBasePath);
                fileInfo.SaveAs(nativeFullPath);

                entity.ImageRelativePath = relativeVirtualPath;
                entity.ImageType = FileHelper.GeFileExtensionNameWithoutDot(fileExtensionName);
            }

            if (this.Request.Params["ImageIsMain"] != null)
            {
                entity.ImageIsMain = Logics.True;
            }
            else
            {
                entity.ImageIsMain = Logics.False;
            }

            if (this.Request.Params["CanUsable"] != null)
            {
                entity.CanUsable = Logics.True;
                entity.ImageStatus = Logics.True;
            }
            else
            {
                entity.CanUsable = Logics.False;
                entity.ImageStatus = Logics.False;
            }

            if (entity.ImageGuid == Guid.Empty)
            {
                entity.CreateTime = DateTime.Now;
                ImageBLL.Instance.Create(entity);
            }
            else
            {
                ImageBLL.Instance.Update(entity);
            }

            string currenReturnUrl = Url.Action("ImageList", new { productGuid = entity.RelativeGuid, returnUrl = returnUrl });
            return new RedirectResult(currenReturnUrl);
        }

        public ActionResult ImageDelete(Guid imageGuid)
        {
            LogicStatusInfo statusInfo = new LogicStatusInfo();
            statusInfo.IsSuccessful= ImageBLL.Instance.Delete(imageGuid);
            return Json(statusInfo);
        }

    }
}
